package org.itboys.store.web;

import org.itboys.commons.CommonConstants;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.framework.query.JsonPageUtils;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.mongodb.utils.page.Page;
import org.itboys.store.constant.ProductConstant;
import org.itboys.store.entity.Order;
import org.itboys.store.service.StoreOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by huml on 2016/4/20.
 * 后台订单控制层
 */
@RestController
@RequestMapping("/store/order")
public class StoreOrderController extends BaseController {

    @Autowired
    private StoreOrderService storeOrderService;

    /**
     * 跳转
     * @param model
     * @return
     */
    @RequestMapping("/orderVM")
    public ModelAndView orderVM(Model model){
        return new ModelAndView("ace/store/order");
    }

    /**
     * 列表
     * @param request
     * @param response
     */
    @RequestMapping("/list")
    public void list(HttpServletRequest request, HttpServletResponse response){
        Page<Order> page = storeOrderService.pageQuery(request);
        JsonPageUtils.renderJsonPage(page.getTotal(), page.getData(), response);
    }

    /**
     * 确认发货
     * @param orderId  订单id
     * @param express  物流公司
     * @param expressNo  运单号
     * @param response
     */
    @RequestMapping("/doSend")
    public void doSend(@RequestParam("orderId")Long orderId,
                       @RequestParam("express")String express,
                       @RequestParam("expressNo")String expressNo,
                       HttpServletResponse response){
        try {
            Order order = storeOrderService.getById(orderId);
            order.setStatus(ProductConstant.OrderStatic.ORDER_STATUS_SEND);
            order.setExpress(express);
            order.setExpressNo(expressNo);
            storeOrderService.update(order);
            AjaxUtils.renderJson(response, CommonConstants.SUCCESS);
        } catch (Exception e) {
            logger.error("/store/order/doSend error", e);
            AjaxUtils.renderJson(response, CommonConstants.FAIL);
        }
    }

}
